/**
 * junixsocket
 * <p>
 * Copyright 2009-2021 Christian Kohlschütter
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.liuyun;

import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;

/**
 * A simple demo client.
 *
 * @author Christian Kohlschütter
 */
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        final File socketFile = new File(new File(System.getProperty("java.io.tmpdir")),
                "junixsocket-test.sock");

        try (AFUNIXSocket sock = AFUNIXSocket.newInstance()) {
            try {
                sock.connect(new AFUNIXSocketAddress(socketFile));
            } catch (SocketException e) {
                System.out.println("Cannot connect to server. Have you started it?");
                System.out.println();
                throw e;
            }
            System.out.println("Connected");

            try (InputStream is = sock.getInputStream(); //
                 OutputStream os = sock.getOutputStream();) {

                byte[] buf = new byte[128];

                int read = is.read(buf);
                System.out.println("Server says: " + new String(buf, 0, read, StandardCharsets.UTF_8));

                System.out.println("Replying to server...");
                os.write("Hello Server".getBytes(StandardCharsets.UTF_8));
                os.flush();

                System.out.println("Now reading numbers from the server...");
                DataInputStream din = new DataInputStream(is);
                DataOutputStream dout = new DataOutputStream(os);
                while (!Thread.interrupted()) {
                    int number = din.readInt();
                    if (number == -123) {
                        // by convention of this demo, if the number is -123, we stop.
                        // If we don't do this, we'll get an EOFException upon the next unsuccessful read.
                        break;
                    }
                    System.out.println(number);

                    int ourNumber = number * 2;

                    System.out.println("Sending back " + ourNumber);
                    dout.writeInt(ourNumber);
                }
            }
        }

        System.out.println("End of communication.");
    }
}
